package com.mj.mes.system.SysLogger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

/**
 * Created by Administrator on 2017/8/21.
 */
@Service
public class SysLoggerService {
    @Autowired
    private SysLoggerDao sysLoggerDao;

    Page<SysLogger> findAll(Pageable pageable) {
        return sysLoggerDao.findAll(pageable);
    }

    Page<SysLogger> findByUserName(String userName, Pageable pageable) {
        Specification<SysLogger> specification = new Specification<SysLogger>() {
            @Override
            public Predicate toPredicate(Root<SysLogger> root,
                                         CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate _name = criteriaBuilder.like(root.get("userName"),"%"+ userName+"%");
                return criteriaBuilder.and(_name);
            }
        };
        return sysLoggerDao.findAll(specification,pageable);
    }

    List<SysLogger> findAll(){
        return sysLoggerDao.findAll();
    }

    public SysLogger findOne(Long id){
        return sysLoggerDao.findOne(id);
    }

    public void addSysLogger(SysLogger sysLogger){
        sysLoggerDao.save(sysLogger);
    }
}


